/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.repair.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.repair.dto.defert.PlanAndSignDateDTO;
import org.springblade.modules.repair.entity.MaintainPlanContent;
import org.springblade.modules.repair.entity.MaintainPlanDefertHistory;
import org.springblade.modules.repair.service.IMaintainPlanContentService;
import org.springblade.modules.repair.service.IMaintainPlanDefertHistoryService;
import org.springblade.modules.repair.vo.MaintainPlanDefertHistoryVO;
import org.springblade.modules.repair.vo.defert.DefertSaveVo;
import org.springblade.modules.repair.vo.defert.PlanAndSignDateVo;
import org.springblade.modules.repair.vo.defert.UpdateUserInfo;
import org.springblade.modules.repair.wrapper.MaintainPlanDefertHistoryWrapper;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 维保任务延期历史日志 控制器
 *
 * @author ysq
 * @since 2022-11-19
 */
@RestController
@AllArgsConstructor
@RequestMapping("repair/maintainplandeferthistory")
@Api(value = "维保任务延期历史日志", tags = "维保任务延期历史日志接口")
public class MaintainPlanDefertHistoryController extends BladeController {

	private final IMaintainPlanDefertHistoryService maintainPlanDefertHistoryService;
	private final IMaintainPlanContentService planContentService;


	/**
	 * 分页 维保任务延期历史日志
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入maintainPlanDefertHistory")
	public R<IPage<MaintainPlanDefertHistoryVO>> list(MaintainPlanDefertHistory maintainPlanDefertHistory, Query query) {
		query.setDescs("create_time");
		IPage<MaintainPlanDefertHistory> pages = maintainPlanDefertHistoryService.page(Condition.getPage(query), Condition.getQueryWrapper(maintainPlanDefertHistory));
		return R.data(MaintainPlanDefertHistoryWrapper.build().pageVO(pages));
	}

	@PostMapping("update")
	public R<Boolean> update(@RequestBody MaintainPlanDefertHistory maintainPlanDefertHistory) {
		final BladeUser user = AuthUtil.getUser();
		if (StrUtil.isNotEmpty(maintainPlanDefertHistory.getDefertImg())) {
			UpdateUserInfo updateUserInfo = new UpdateUserInfo();
			updateUserInfo.setUpdateTime(new Date());
			updateUserInfo.setUserId(user.getUserId());
			updateUserInfo.setUserName(user.getNickName());
			maintainPlanDefertHistory.setUserDefertImg(JSON.toJSONString(updateUserInfo));
		}
		if (StrUtil.isNotEmpty(maintainPlanDefertHistory.getDefertDesc())) {
			UpdateUserInfo updateUserInfo = new UpdateUserInfo();
			updateUserInfo.setUpdateTime(new Date());
			updateUserInfo.setUserId(user.getUserId());
			updateUserInfo.setUserName(user.getNickName());
			maintainPlanDefertHistory.setUserDefertDesc(JSON.toJSONString(updateUserInfo));
		}


		return R.data(maintainPlanDefertHistoryService.updateById(maintainPlanDefertHistory));
	}


	/**
	 * 新增 维保任务延期历史日志
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增")
	public R<Boolean> save(@Valid @RequestBody DefertSaveVo defertSaveVo) {
		maintainPlanDefertHistoryService.save(defertSaveVo);
		MaintainPlanContent planContent = planContentService.getById(defertSaveVo.getPlanId());
		List<Long> elevatorIds = new ArrayList<>();
		elevatorIds.add(planContent.getElevatorId());
		planContentService.updateElevatorStrategy(elevatorIds);
		return R.data(Boolean.TRUE);
	}


	/**
	 * 获取考勤和工单日历
	 *
	 * @param planAndSignDateDTO
	 * @return
	 */
	@PostMapping("/getPlanAndSignDates")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "获取考勤和工单日历")
	public R<List<PlanAndSignDateVo>> getPlanAndSignDates(@RequestBody PlanAndSignDateDTO planAndSignDateDTO) {

		planAndSignDateDTO.setTenantId(AuthUtil.getTenantId());
		return R.data(maintainPlanDefertHistoryService.getPlanAndSignDates(planAndSignDateDTO));
	}

}
